package cn.admin.business.controller.staff;

import cn.admin.business.controller.staff.verify.LoginParam;
import cn.admin.common.domain.Result;
import cn.admin.system.staff.app.SysUserApp;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @author YJ
 * @DateTime 2023/2/21 13:48
 */
@RestController
@RequestMapping("/admin/")
public class LoginController {

    @Autowired
    private SysUserApp sysUserApp;

    /**
     * 员工登录
     *
     * @param loginParam 登录参数
     * @return true || false
     */
    @PostMapping("/login")
    public Result<String> login(@Validated @RequestBody LoginParam loginParam) {
        String login = sysUserApp.login(loginParam.getUsername(), loginParam.getPassword(), loginParam.getCode(), loginParam.getUuid());
        return Result.success(login);
    }


    public static void main(String[] args) throws Exception {
        System.out.println(deEncodeByPath("http://182.61.145.98:9000/wxscrmplus4j/2023/08/03/cea1c7d622b040df8491d2fd446bf1f1.png"));
    }


    public static String deEncodeByPath(String path) throws Exception {
        BufferedImage image;
        URL url = new URL(path);
        image = ImageIO.read(url);
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
        com.google.zxing.Result result = new MultiFormatReader().decode(binaryBitmap, new HashMap<>() {{
            put(DecodeHintType.CHARACTER_SET, "UTF-8");
        }});
        System.out.println("图片中内容 : " + result.getText());
        return result.getText();
    }
}
